home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / prg_casm / wsc4c10.zip / LOGIN.C < prev    next >
Text File  |  1996-09-08  |  11KB  |  402 lines

  1. /*
  2. **                    --- login.c ---
  3. **
  4. **  EXAMPLE CODE: Very login terminal emulator.
  5. **
  6. */
  7.  
  8. #define USECOMM
  9.  
  10. #include "windows.h"
  11. #include "login.h"
  12. #include "message.h"
  13. #include "wsc.h"
  14. #include "ascii.h"
  15. #include "config.h"
  16. #include "paint.h"
  17. #include "line.h"
  18. #include "menu.h"
  19. #include "modem_io.h"
  20. #include "about.h"
  21. #include "wscerror.h"
  22.  
  23. /* defines */
  24.  
  25. #define Handshake_1  11
  26. #define Handshake_2  12
  27. #define Handshake_3  13
  28. #define Dial_1  21
  29. #define Dial_2  22
  30. #define Dial_3  23
  31.  
  32. /* public globals */
  33.  
  34. HWND hMainWnd;            /* main window handle */
  35. HWND hInfoWnd;            /* popup handle */
  36. HANDLE hInstance;         /* program instance */
  37. int OnLineFlag = FALSE;   /* TRUE: online */
  38. int FatalFlag = FALSE;    /* TRUE: fatal error */
  39. char Temp[1024];
  40.  
  41. /* private globals */
  42.  
  43. static int WinWidth = 8 * NCOLS;
  44. static int WinHeight = 12 * NROWS + 48;
  45.  
  46. /* miscellaneous functions */
  47.  
  48. void ErrorCheck(int);
  49. void ErrorMessage(char *);
  50.  
  51. int PASCAL WinMain(HANDLE hInst,HANDLE hPrevInstance,
  52.                    LPSTR lpCmdLine,int nCmdShow)
  53. {WNDCLASS  wc;
  54.  MSG msg;
  55.  BOOL Result;
  56.  if(!hPrevInstance)
  57.    {/* register main window class */
  58.     wc.style = CS_HREDRAW | CS_VREDRAW;
  59.     wc.lpfnWndProc = MainWndProc;
  60.     wc.cbClsExtra = 0;
  61.     wc.cbWndExtra = 0;
  62.     wc.hInstance = hInst;
  63.     wc.hIcon = LoadIcon(hInst, "LoginIcon");
  64.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  65.     wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  66.     wc.lpszMenuName =  "LoginMenu";
  67.     wc.lpszClassName = "LoginWClass";
  68.     Result = RegisterClass(&wc);
  69.     if(!Result) return FALSE;
  70.    }
  71.  
  72.  /* create main window */
  73.  hInstance = hInst;
  74.  hMainWnd = CreateWindow(
  75.         "LoginWClass",   "Login",       WS_OVERLAPPEDWINDOW,
  76.         CW_USEDEFAULT,    CW_USEDEFAULT,
  77.         WinWidth,         WinHeight,
  78.         NULL,             NULL,
  79.         hInstance,        NULL);
  80.  ShowWindow(hMainWnd, nCmdShow);
  81.  UpdateWindow(hMainWnd);
  82.  
  83.  /* window control loop */
  84.  
  85.  while(GetMessage(&msg,NULL,NULL,NULL))
  86.    {
  87.     TranslateMessage(&msg);
  88.     DispatchMessage(&msg);
  89.    }
  90.  return (msg.wParam);
  91. } /* end WinMain */
  92.  
  93. long FAR PASCAL MainWndProc(HWND hWindow,UINT message,WPARAM wParam,LPARAM lParam)
  94. {int  i;
  95.  int  TheChar;
  96.  int  Count;
  97.  int  rc, Code;
  98.  HDC  hDC;
  99.  UINT idTimer;
  100.  PAINTSTRUCT ps;
  101.  static FARPROC lpProcAbout;
  102.  static int ThePort;
  103.  static int mioState;
  104.  
  105.  hMainWnd = hWindow;
  106.  switch (message)
  107.     {case WM_COMMAND:
  108.          switch(wParam)
  109.            {case MSG_ABOUT:
  110.               DialogBox(hInstance,"AboutBox",hMainWnd,lpProcAbout);
  111.               break;
  112.  
  113.  
  114.             case MSG_BREAK:
  115.               mioBreak(ThePort);
  116.               EnableTheMenu(MSG_BREAK);
  117.               mioState = 0;
  118.               break;
  119.  
  120.             case MSG_HANDSHAKE:
  121.               if(!OnLineFlag) DisplayLine("Must be online!");
  122.               else
  123.                 {mioState = Handshake_1;
  124.                  EnableTheMenu(MSG_BREAK);
  125.                 }
  126.               break;
  127.  
  128.             case MSG_DIAL:
  129.               if(!OnLineFlag) DisplayLine("Must be online!");
  130.               else
  131.                 {mioState = Dial_1;
  132.                  EnableTheMenu(MSG_BREAK);
  133.                 }
  134.               break;
  135.  
  136.  
  137.             /*************/
  138.  
  139.             case MSG_DEBUG:
  140.               break;
  141.  
  142.             case MSG_ONLINE:
  143.               if(FatalFlag) ErrorMessage("Fatal Error");
  144.               else
  145.                 {/* try to go on-line */
  146.                  ThePort = GetPort();
  147.                  GoOnLine(ThePort);
  148.                  SetTitle();
  149.                  CheckTheMenu(MSG_ONLINE);
  150.                  UncheckTheMenu(MSG_OFFLINE);
  151.                  EnableTheMenu(MSG_OFFLINE);
  152.                  DisableTheMenu(MSG_ONLINE);
  153.                  for(i=0;i<4;i++) DisableTheMenu(MSG_COM1+i);
  154.                  DrawMenuBar(hMainWnd);
  155.                 }
  156.               break;
  157.  
  158.             case MSG_OFFLINE:
  159.  
  160.               CheckTheMenu(MSG_OFFLINE);
  161.               UncheckTheMenu(MSG_ONLINE);
  162.               EnableTheMenu(MSG_ONLINE);
  163.               DisableTheMenu(MSG_OFFLINE);
  164.               for(i=0;i<4;i++) EnableTheMenu(MSG_COM1+i);
  165.               GoOffLine(ThePort);
  166.               SetTitle();
  167.               DrawMenuBar(hMainWnd);
  168.               break;
  169.  
  170.             case MSG_EXIT:
  171.               GoOffLine(ThePort);
  172.               KillTimer(hMainWnd,idTimer);
  173.               PostQuitMessage(0);
  174.               break;
  175.  
  176.             case MSG_110:
  177.               SetBaud(Baud110);
  178.               break;
  179.  
  180.             case MSG_300:
  181.               SetBaud(Baud300);
  182.               break;
  183.  
  184.             case MSG_1200:
  185.               SetBaud(Baud1200);
  186.               break;
  187.  
  188.             case MSG_2400:
  189.               SetBaud(Baud2400);
  190.               break;
  191.  
  192.             case MSG_4800:
  193.               SetBaud(Baud4800);
  194.               break;
  195.  
  196.             case MSG_9600:
  197.               SetBaud(Baud9600);
  198.               break;
  199.  
  200.             case MSG_19200:
  201.               SetBaud(Baud19200);
  202.               break;
  203.  
  204.             case MSG_38400:
  205.               SetBaud(Baud38400);
  206.               break;
  207.  
  208.             case MSG_57600:
  209.               SetBaud(Baud57600);
  210.               break;
  211.  
  212.             case MSG_COM1:
  213.               SetPort(COM1);
  214.               break;
  215.  
  216.             case MSG_COM2:
  217.               SetPort(COM2);
  218.               break;
  219.  
  220.             case MSG_COM3:
  221.               SetPort(COM3);
  222.               break;
  223.  
  224.             case MSG_COM4:
  225.               SetPort(COM4);
  226.               break;
  227.  
  228.             case MSG_NONE:
  229.               SetParity(NoParity);
  230.               break;
  231.  
  232.             case MSG_EVEN:
  233.               SetParity(EvenParity);
  234.               break;
  235.  
  236.             case MSG_ODD:
  237.               SetParity(OddParity);
  238.               break;
  239.  
  240.             case MSG_1_SB:
  241.               SetStopBits(OneStopBit);
  242.               break;
  243.  
  244.             case MSG_2_SB:
  245.               SetStopBits(TwoStopBits);
  246.               break;
  247.  
  248.             case MSG_7_DB:
  249.               SetWordLength(WordLength7);
  250.               break;
  251.  
  252.             case MSG_8_DB:
  253.               SetWordLength(WordLength8);
  254.               break;
  255.  
  256.             default:
  257.               return (DefWindowProc(hMainWnd, message, wParam, lParam));
  258.            }
  259.          break;
  260.  
  261.     case WM_CREATE:
  262.  
  263.       /* check "OFFLINE" menu item */
  264.       CheckTheMenu(MSG_OFFLINE);
  265.       DisableTheMenu(MSG_OFFLINE);
  266.       /* create AboutDlgProc() thunk */
  267.       lpProcAbout = MakeProcInstance(AboutDlgProc, hInstance);
  268.       /* initialize paint module */
  269.       InitPaint();
  270.       /* init configuration */
  271.       CheckAll();
  272.       SetText((LPSTR)"LOGIN");
  273.       SetTitle();
  274.       /* start timer */
  275.       idTimer = SetTimer(hMainWnd,1,125,NULL);
  276.       if(idTimer==0)
  277.          {ErrorMessage("No timers remaining !");
  278.           FatalFlag = TRUE;
  279.          }
  280.       break;
  281.  
  282.     case WM_CHAR:
  283.       SioPutc(ThePort, (char)wParam );
  284.       break;
  285.  
  286.     case WM_TIMER:
  287.       /* fatal error ? */
  288.       if(FatalFlag) break;
  289.       if(!OnLineFlag) break;
  290.       if(mioState)
  291.         {/* MIO is running ! */
  292.          rc = mioDriver(ThePort);
  293.          if(rc==MIO_IDLE)
  294.            {/* time to go to next MIO state (since driver is idle) */
  295.             switch(mioState)
  296.               {case Handshake_1:
  297.                  /* send "AT" to modem */
  298.                  Code = mioSendTo(ThePort, 125, "!AT!");
  299.                  mioState = Handshake_2;
  300.                  break;
  301.                case Handshake_2:
  302.                  /* expect "OK" back */
  303.                  Code = mioWaitFor(ThePort, 3000, 1, "OK");
  304.                  mioState = Handshake_3;
  305.                  break;
  306.                case Handshake_3:
  307.                  /* did we get expected result ("OK") */
  308.                  if(mioResult(ThePort)) DisplayString (">>>OK was received");
  309.                  else DisplayString (">>>OK was NOT received!");
  310.                  /* all done */
  311.                  EnableTheMenu(MSG_BREAK);
  312.                  mioState = 0;
  313.                  break;
  314.                case Dial_1:
  315.                  /* dial modem */
  316.                  Code = mioSendTo(ThePort, 125, "!ATDT880,9748!");
  317.                  mioState = Dial_2;
  318.                  break;
  319.                case Dial_2:
  320.                  /* expect "CONNECT" back (wait up to 60 seconds) */
  321.                  if(mioWaitFor(ThePort, 60000, 1, "CONNECT")) mioState = Dial_3;
  322.                  else DisplayString (">>>mioWaitFor fails!");
  323.                  break;
  324.                case Dial_3:
  325.                  /* did we get expected result ("CONNECT") */
  326.                  if(mioResult(ThePort)) DisplayString (">>>CONNECT was received");
  327.                  else DisplayString (">>>CONNECT was NOT received!");
  328.                  /* all done */
  329.                  EnableTheMenu(MSG_BREAK);
  330.                  mioState = 0;
  331.               } /* end-switch */
  332.            }
  333.          else
  334.            {/* MIO is not IDLE */
  335.             if(rc != MIO_RUNNING) DisplayChar((char)rc);
  336.            }
  337.         } /* end-if(mioState) */
  338.       else
  339.         {/* MIO is not running */
  340.          Count = 0;
  341.          /* fetch line of up to 1024 chars */
  342.          for(i=0;i<1024;i++)
  343.            {TheChar = SioGetc(ThePort);
  344.             /* character available ? */
  345.             if(TheChar==WSC_NO_DATA) break;
  346.             Temp[Count++] = TheChar;
  347.             /*if((char)TheChar==(char)LF) break;*/
  348.            } /* end while */
  349.          if(Count>0) WriteTheString(Temp,Count);
  350.         }
  351.       break;
  352.  
  353.  
  354.     case WM_SETFOCUS:
  355.       /* create client area caret */
  356.       CreateCaret(hMainWnd,NULL,3,10);
  357.       SetCaretPos(GetXposition(),GetYposition());
  358.       ShowCaret(hMainWnd);
  359.       ShowCaret(hMainWnd);
  360.       break;
  361.  
  362.     case WM_KILLFOCUS:
  363.       DestroyCaret();
  364.       break;
  365.  
  366.     case WM_PAINT:
  367.       HideCaret(hMainWnd);
  368.       hDC = BeginPaint(hMainWnd, &ps);
  369.       SetMapMode(hDC,MM_ANISOTROPIC);
  370.       SelectObject(hDC, GetStockObject(OEM_FIXED_FONT) );
  371.       PaintMain(hDC,&ps);
  372.       EndPaint(hMainWnd,&ps);
  373.       SetCaretPos(GetXposition(),GetYposition());
  374.       ShowCaret(hMainWnd);
  375.       break;
  376.  
  377.     case WM_DESTROY:
  378.       GoOffLine(ThePort);
  379.       if(idTimer) KillTimer(hMainWnd,idTimer);
  380.       PostQuitMessage(0);
  381.       break;
  382.  
  383.     default:
  384.       return (DefWindowProc(hMainWnd, message, wParam, lParam));
  385.    }
  386.  return (NULL);
  387. } /* end MainWndProc */
  388.  
  389. void ErrorCheck(int Code)
  390. {/* trap PCL error codes */
  391.  if(Code<0)
  392.      {SioError(Code,"Sio Error");
  393.       SioDone(GetPort());
  394.       FatalFlag = TRUE;
  395.      }
  396. }
  397.  
  398. void ErrorMessage(char *MsgPtr)
  399. {
  400.  MessageBox(hMainWnd,MsgPtr,"ERROR",MB_ICONEXCLAMATION | MB_OK);
  401. }
  402.